import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { maglist, liuyan } from '../serives/msgboard/msgboard';
export interface IndexModelState {
  name: string;
  list:[],
  liuyanlist:[]
}

export interface IndexModelType {
  namespace: 'msgboard';
  state: IndexModelState;
  effects: {
    getlist: Effect;
    liuyan:Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const msgboard: IndexModelType = {
  //命名空间
  namespace: 'msgboard',
 
  state: {
    name: '',
    list:[],
    liuyanlist:[]
  },

  effects: {
    *getlist({ payload }, { call, put }) {
        let res=yield call(maglist)
        //console.log(res); 
        yield put({ 
          type:'save',
          payload:{
            list:res.data
          }
        })
    },
    *liuyan({ payload }, { call, put }) {
      let res=yield call(liuyan)
      console.log(res); 
      yield put({ 
        type:'save',
        payload:{
          liuyanlist:res.data[0]
        }
      })
  },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          });
        }
      });
    }
  },
};

export default msgboard;